package org.vectrics.system;

import java.util.Date;
import java.util.Locale;

import org.apache.log4j.Logger;
import org.vectrics.SystemException;
import org.vectrics.common.db.CodedObject;
import org.vectrics.common.db.RecordObject;
import org.vectrics.domain.Domain;
import org.vectrics.provider.CoreServiceLocator;

/**
 * @author mmoore
 */
public class SystemConstant implements RecordObject, CodedObject {
    public static String TYPE_DATE = "date";

    public static String TYPE_DATE_TIME = "datetime";

    public static String TYPE_DOUBLE = "double";

    public static String TYPE_INTEGER = "integer";

    public static String TYPE_LONG = "long";

    public static String TYPE_STRING = "string";

    private boolean canPassToChildren = true;

    private String code = null;

    private String description = null;

    private Domain domain = null;

    private boolean global = false;

    private Long id = null;

    private transient final Logger log = Logger.getLogger(this.getClass());

    private String type = null;

    private String value = null;

    /**
	 * 
	 */
    public SystemConstant() {
        super();

    }

    public Boolean getBooleanValue() {
        Boolean boolValue = new Boolean(this.getValue().trim());
        if (this.log.isDebugEnabled()) {
            this.log.debug("getBooleanValue() - code=" + this.code + "  raw value=" + this.getValue()
                    + "  bool value = " + boolValue.toString());
        }
        return (boolValue);
    }

    public boolean getCanPassToChildren() {
        return this.canPassToChildren;
    }

    /**
     * @return Returns the code.
     */
    public String getCode() {
        return this.code;
    }

    public Date getDateTimeValue() {
        Date date = null;
        try {
            Locale locale = CoreServiceLocator.getConfiguration().getDefaultLocale();
            date = CoreServiceLocator.getDateFormat().getDateFormatRoundToMinute(locale).parse(this.getValue().trim());
        } catch (Exception ex) {
            throw new SystemException("Exception parsing date: " + this.getValue(), ex);
        }
        return (date);
    }

    public Date getDateValue() {
        Date date = null;
        try {
            Locale locale = CoreServiceLocator.getConfiguration().getDefaultLocale();
            date = CoreServiceLocator.getDateFormat().getDateFormatRoundToDay(locale).parse(this.getValue().trim());
        } catch (Exception ex) {
            throw new SystemException("Exception parsing date: " + this.getValue(), ex);
        }
        return (date);
    }

    /**
     * @return Returns the description.
     */
    public String getDescription() {
        return this.description;
    }

    /**
     * @return Returns the domain.
     */
    public Domain getDomain() {
        return this.domain;
    }

    public Double getDoubleValue() {
        return (new Double(this.getValue().trim()));
    }

    /**
     * @return Returns the global.
     */
    public boolean getGlobal() {
        return this.global;
    }

    /**
     * @return Returns the id.
     */
    public Long getId() {
        return this.id;
    }

    public Integer getIntegerValue() {
        return (new Integer(this.getValue().trim()));
    }

    public Long getLongValue() {
        return (new Long(this.getValue()));
    }

    public String getStringValue() {
        return this.value;
    }

    /**
     * @return Returns the type.
     */
    public String getType() {
        return this.type;
    }

    /**
     * @return Returns the value.
     */
    public String getValue() {
        return this.value;
    }

    public void setCanPassToChildren(boolean canPassToChildren) {
        this.canPassToChildren = canPassToChildren;
    }

    /**
     * @param code
     *            The code to set.
     */
    public void setCode(String code) {
        this.code = code;
    }

    /**
     * @param description
     *            The description to set.
     */
    public void setDescription(String description) {
        this.description = description;
    }

    /**
     * @param domain
     *            The domain to set.
     */
    public void setDomain(Domain domain) {
        this.domain = domain;
    }

    /**
     * @param global
     *            The global to set.
     */
    public void setGlobal(boolean global) {
        this.global = global;
    }

    /**
     * @param id
     *            The id to set.
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * @param type
     *            The type to set.
     */
    public void setType(String type) {
        this.type = type;
    }

    /**
     * @param value
     *            The value to set.
     */
    public void setValue(String value) {
        this.value = value;
    }

}
